// pages/post/post.js//
const DB = wx.cloud.database().collection("List")
Page({
  data: {
    choose: false,
    DqOpenid: '',
    grade_name1: '类型',
    grades1: ['运动', '学习', '出行', '游戏'],
    //时间
    date: '',
    start: '2022-01-01',
    title: '',
    end: '',
    //说说
    img_url: [],
    content: '',
    imgid: [],
  },
  // 点击下拉框 
  bindShowMsg1() {
    this.setData({
      choose: !this.data.choose
    })
  },
  // 已选下拉框 
  mySelect1(e) {
    //console.log(e)
    var name = e.currentTarget.dataset.name
    this.setData({
      grade_name1: name,
      choose: false
    })
  },
  //测试是否登录
  onShow:function (res){
    let app = getApp();
    if(!app.globalData.hasLogin)
    {
      setTimeout(
        function () { //注意function这里不能缺少
          wx.switchTab({
            url: '../my/my',
          })
        }, 1500)
      wx.showToast({
        title: '请登录后再发布',
        icon:'error'
      })
    }
  },
  //时间
  onLoad: function (options) {
    let now = new Date();
    let year = now.getFullYear();
    let month = now.getMonth() + 1;
    this.setData({
      end: '' + year + '-' + (Array(2).join(0) + month).slice(-2) + '-' + (Array(2).join(0) + now.getDate()).slice(-2),
    })
  },
  bindDateChange: function (e) {
    //console.log(e.detail.value)
    this.setData({
      //日期
      date: e.detail.value
    })
  },
  //说说
  input: function (e) {
    this.setData({
      content: e.detail.value
    })
  },
  input1: function (e) {
    this.setData({
      title: e.detail.value
    })
  },
  chooseimage: function () {
    var that = this;
    wx.chooseImage({
      count: 9, // 默认9  
      sizeType: ['original', 'compressed'], // 可以指定是原图还是压缩图，默认二者都有  
      sourceType: ['album', 'camera'], // 可以指定来源是相册还是相机，默认二者都有  
      success: function (res) {
        if (res.tempFilePaths.length > 0) {
          //图如果满了5张，不显示加图
          if (res.tempFilePaths.length == 9) {
            that.setData({
              hideAdd: 1
            })
          } else {
            that.setData({
              hideAdd: 0
            })
          }
          //把每次选择的图push进数组
          let img_url = that.data.img_url;
          for (let i = 0; i < res.tempFilePaths.length; i++) {
            img_url.push(res.tempFilePaths[i])
          }
          that.setData({
            img_url: img_url
          })
          //console.log(img_url);
        }
      }
    })
  },
  //发布按钮事件
  send: function () {
    var that = this;
    var user_id = wx.getStorageSync('userid')
    //console.log(res);
    if (that.data.grade_name1 == '类型' || (that.data.title == false) || (that.data.date == false) || (that.data.content == false) || (that.data.img_url == false)) {
      wx.showToast({
        title: '请填写每项内容',
        icon: 'error',
        duration: 2000
      })
      return;
    }
    wx.showLoading({
      title: '上传中',
    })
    that.img_upload();
  },
  //图片上传
  img_upload: function () {
    let that = this;
    let img_url = that.data.img_url;
    let img = [];
    //由于图片只能一张一张地上传，所以用循环
    for (let i = 0; i < img_url.length; i++) {
      wx.cloud.uploadFile({
        cloudPath: (new Date()).getTime() + Math.floor(9 * Math.random()) + ".png", // 上传至云端的路径
        filePath: img_url[i], // 小程序临时文件路径
        success: res => {
          // 返回文件 ID
          img.push(res.fileID);
          if (img.length == img_url.length) {
            that.setData({
              imgid: img
            });
            let app = getApp();
            const res = {
              avatarUrl:app.globalData.avatarUrl,
              username:app.globalData.name,
              name: that.data.grade_name1,
              data: that.data.date,
              title: that.data.title,
              content: that.data.content,
              images: that.data.imgid,
              isok:false,
            };
            DB.add({
              data: res,
              success: function (e) {
                wx.hideLoading({
                  success: (res) => {
                    wx.showToast({
                      title: '发布成功',
                    })
                    setTimeout(
                      function () { //注意function这里不能缺少
                        wx.navigateBack({
                          delta: 1,
                        })
                      }, 1000)
                  },
                })
              },
              fail: function (e) {
                wx.hideLoading({
                  success: (res) => {
                    wx.showToast({
                      title: '发布失败',
                      icon: 'error',
                    })
                  },
                })
              }
            })
          }
        },
        fail: res => {
          wx.hideLoading({
            success: (res) => {
              wx.showToast({
                title: '上传失败',
                icon: 'error',
              })
            },
          })
        }
      })
    }
  }
})